package com.ctg.payroll2.dao.social.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.ctg.payroll2.bean.filter.social.PbCommsocialMasterFilter;
import com.ctg.payroll2.bean.filter.social.PbSocialMasterFilter;
import com.ctg.payroll2.bean.po.social.PbCommsocialDetail;
import com.ctg.payroll2.bean.po.social.PbCommsocialMaster;
import com.ctg.payroll2.bean.po.social.RecomTemplateSocialItemSearchBean;
import com.ctg.payroll2.dao.social.RecomTemplateDao;
import com.ctg.platform.orm.ibatis.IbatisEntityDao;
import com.ctg.platform.util.Page;

public class RecomTemplateDaoImpl extends IbatisEntityDao implements RecomTemplateDao{
	private String getNameSpace(){
		return getClass().getSimpleName();
	}
	public void deletePbCommsocialById(Long id) {
		getSqlMapClientTemplate().delete(getNameSpace()+".deletePbCommsocialById",id);
	}

	public void deletePbCommsocialByIds(String ids) {
		getSqlMapClientTemplate().delete(getNameSpace()+".deletePbCommsocialByIds", ids);
	}

	public void deleteRecomTemplateById(Long id) {
		getSqlMapClientTemplate().delete(getNameSpace()+".deleteRecomTemplateDetailById", id);
		getSqlMapClientTemplate().delete(getNameSpace()+".deleteRecomTemplateById", id);
	}

	public void deleteRecomTemplateByIds(String ids) {
		getSqlMapClientTemplate().delete(getNameSpace()+".deleteRecomTemplateDetailByIds", ids);
		getSqlMapClientTemplate().delete(getNameSpace()+".deleteRecomTemplateByIds", ids);
	}

	@SuppressWarnings("unchecked")
	public List<PbCommsocialMaster> getPbSocialMasterListById(Long id) {
		return getSqlMapClientTemplate().queryForList(getNameSpace() + ".getPbSocialMasterById", id);
	}
	
	@SuppressWarnings("unchecked")
	public List<RecomTemplateSocialItemSearchBean> getSocialItemByTemplateId(Long id){
		return getSqlMapClientTemplate().queryForList(getNameSpace() + ".getSocialItemByTemplateId", id);
	}

	public void insertPbCommsocialDetail(PbCommsocialDetail detail) {
		getSqlMapClientTemplate().insert(getNameSpace() + ".insertPbCommsocialDetail", detail);
	}

	public void insertRecomTemplate(PbCommsocialMaster pbCommsocialMaster) {
		Long l;
		try {
			l = (Long)getSqlMapClient().insert(getNameSpace() + ".insertRecomTemplate", pbCommsocialMaster);
		
			if (StringUtils.isNotEmpty(pbCommsocialMaster.getSocialItemIds().trim())) {
				String[] str = pbCommsocialMaster.getSocialItemIds().split(",");
				for (int i = 0; i < str.length; i++) {
					PbCommsocialDetail detail = new PbCommsocialDetail();
					detail.setSocial_security_id(Long.parseLong(str[i]));
					detail.setTemplate_id(l);
					getSqlMapClient().insert(getNameSpace() + ".insertPbCommsocialDetail", detail);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void fakeDeleteRecomTemplateById(Long id){
		getSqlMapClientTemplate().update(getNameSpace()+".fakeDeleteRecomTemplateById", id);
	}

	public Page queryRecomTemplate(PbCommsocialMasterFilter filter) {
		return pagedQueryByFilter(this.getClass(), filter,
				".queryRecomTemplate_count",
				".queryRecomTemplate_query");
	}

	public Page querySocialItemAll(PbSocialMasterFilter filter) {
		return pagedQueryByFilter(this.getClass(), filter,
				".querySocialItemAll_count",
				".querySocialItemAll_query");
	}

	public void updateRecomTemplate(PbCommsocialMaster pbCommsocialMaster) {
		if (pbCommsocialMaster == null) {
			return;
		}
		getSqlMapClientTemplate().update(getNameSpace()+".updateRecomTemplate", pbCommsocialMaster);
		String socialItemIds = pbCommsocialMaster.getSocialItemIds();
		if(socialItemIds!=null){
			PbCommsocialDetail detail = new PbCommsocialDetail();
			Long masterId = pbCommsocialMaster.getTemplate_id();
			getSqlMapClientTemplate().delete(getNameSpace()+".deletePbCommsocialDetailByTemplateId", masterId);
			if("".equals(socialItemIds.trim())){
				return;
			}
			else{
				detail.setTemplate_id(masterId);
				String[] ids = socialItemIds.split(",");
				for (int i = 0; i < ids.length; i++) {
					detail.setSocial_security_id(Long.parseLong(ids[i]));
					getSqlMapClientTemplate().insert(getNameSpace()+".insertPbCommsocialDetail", detail);
				}
			}
		}
	}
	
}
